#ifndef _APP_H_
#define _APP_H_
#include "stm32l0xx_hal.h"

#define OTA_RESET_MANAGER_ENABLE   0x01

#define CRC_OK     0x01
#define CRC_ERROR  0x00

#define OTA_WRITE_FLASH_PAGE_NUMBER  1
#define OTA_WRITE_FLASH_WORD_NUMBER  OTA_WRITE_FLASH_PAGE_NUMBER * 32
#define OTA_WRITE_FLASH_BYTE_NUMBER  OTA_WRITE_FLASH_WORD_NUMBER * 4
#define OTA_CRC_BYTE_NUMBER    128


#define HTONS(n) ((((uint16_t)((n) & 0x00ff)) << 8) |   \
                  (((uint16_t)(n) & 0xff00) >> 8))

#define HTONL(n) ((((uint32_t)((n) & 0x000000ff)) << 24) | \
                  (((uint32_t)((n) & 0x0000ff00)) << 8) | \
                  (((uint32_t)((n) & 0x00ff0000)) >> 8) | \
                  (((n) & 0xff000000) >> 24))




#define LP_TEST_TYPE                0x0000
/*data pointer type*/
#define APP_DATA_HEAD_TYPE          0x0001
#define APP_DATA_TAIL_TYPE          0x0002
/*time stamp*/
#define TIME_STAMP_TYPE             0x0003
/*data type*/
#define APP_SENSOR_TEMP_HUMI_TYPE      0x0101
#define APP_LED_TYPE                   0x0102
#define APP_SENSOR_VIBERATE_TYPE       0x0103
#define APP_SENSOR_TILT_ANGLE_TYPE     0x0104
#define APP_SENSOR_FFT_TYPE            0x0105
#define APP_SENSOR_AVERAGE_TYPE        0x0106
#define APP_SENSOR_P2P_TYPE            0x0107
#define APP_SENSOR_RAW_DATA_TYPE       0x0108
/*bluenrg type*/
#define APP_BLUENRG_CONFIG_DATA_TYPE   0x0109
/*low power interval type*/
#define LP_WAKEUP_INTERVAL_TYPE        0x0201
/*task type*/
#define TASK_CURRENT_TASK_TYPE         0x0301

#define APP_SENSOR_TEMP_HUMI_LEN       4
#define APP_SENSOR_VIBERATE_LEN        12
#define APP_SENSOR_P2P_TYPE_LEN        12
#define APP_SENSOR_AVERAGE_LEN         6

#define APP_SNESOR_RECORD_WORD_LEN          4        /*word*/


typedef struct _Adv_manufactory_data_t{
 uint8_t  len;
 uint8_t  adv_type;
 uint8_t  company_id[2];
 uint8_t  data_type[2];
 uint8_t  data[];
} adv_manufactory_data_t;

typedef enum {
  head_address_bkp_reg = 0x00,
  tail_address_bkp_reg,
}eeprom_head_tail_type_t;

typedef enum {
 app_ok = 0x00,
 app_error
}APP_Status_t;

typedef enum {
    APP_WAKEUP_STATES_SLEEP          = 0x00,
    APP_WAKEUP_STATES_STOP,
    APP_WAKEUP_STATES_RTC,
    APP_WAKEUP_STATES_3AXIS,
    APP_WAKEUP_STATES_TEMP_HUM,
} APP_STATE_t;


/*LOW POWER*/
void app_3axis_int_callback(void);
void app_wakeup_handle(void);
/*dma*/
void app_i2c_dma_rx_cmp_callback(uint8_t* data);
/*led toggle*/
void app_led_toggle_times(uint8_t times);
/*gatt update callback*/
void app_update_char_value_cb(uint8_t status);
/*eeprom data block option*/
void app_record_cache_store(void* data);

void eeprom_data_block_init(void);
uint32_t get_eeprom_data_write_address_from_BKP(void);
void set_eeprom_data_write_address_to_BKP(uint32_t data);
/*test*/
void eeprom_write_test(void);
#endif //_APP_H_

